var api = require('../../utils/api.js')
var app = getApp();
var util = require('../../utils/util.js');

Page({
  data: {
    currentTime: 0, //日期筛选参数,0累计 1今日 2本周 3本月 4本年
    times: ['累计', '今日', '本周', '本月', '本年'],
    showDateChoose: true, //是否显示日期筛选
    sales: [], //明细列表
    showSaleDetail: true, //是否显示收入明细列表
    inputDateHidden: true, //是否隐藏自定义日期输入框
    endDate: "",
    startDate: "",
    page: 1,
    limit: 20,
    total_page: 1,
		showRegulations: false,
		income_statement: '暂无',
    showStoreRank: true,
    rank_title_select: 2,
    rank_type: 11
  },

  onLoad: function(options) {
    this.setData({
      shop_id: options.id,
      shop_type: options.type,
    })
    wx.setNavigationBarTitle({
      title: options.name
    })
  },

  onShow: function() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };
    if (day < 10) {
      day = '0' + day;
    };
    var formatDate = year + '-' + month + '-' + day;
    this.setData({
      endDate: formatDate,
      startDate: "请选择开始日期",
      ofEndDate: formatDate,
    })
    this.refreshData();
  },

  //刷新数据
  refreshData: function() {
    var that = this;
    this.setData({
      page: 1,
      total_page: 1
    })
    setTimeout(function() {
      that.loadShopSaleStatisticsData();
      that.loadShopSaleDetailData();
    }, 300)
  },

  // 收入统计
  loadShopSaleStatisticsData: function() {
    var that = this;
    var params = new Object()
    params.time = that.data.currentTime;
    if (that.data.currentTime == 0) {
      params.start_time = that.data.startDate;
      params.end_time = that.data.endDate;
    }
    params.id = that.data.shop_id;
    params.type = that.data.shop_type;
    var url = app.globalData.base_url + 'shop/merchantSaleStatistics';
    api.POST({
      url: url,
      params: params,
      success: function(res) {
        util.hiddenLoading();
        that.setData({
          saleSatistics: res.data.data,
        })
      },
    })
  },

  // 收入明细
  loadShopSaleDetailData: function() {
    var that = this;
    var params = new Object()
    params.page = that.data.page;
    params.limit = that.data.limit;
    params.time = that.data.currentTime;
    if (that.data.currentTime == 0) {
      params.start_time = that.data.startDate;
      params.end_time = that.data.endDate;
    }
    params.id = that.data.shop_id;
    params.type = that.data.shop_type;
    //运营商
    params.group_type = that.data.rank_title_select;
    var rank_type = String(that.data.rank_type);
    params.sort_field = rank_type.substring(0, 1);
    params.sort_type = rank_type.substring(1);
    var url = app.globalData.base_url + 'shop/merchantIncomeStatistics';

    api.POST({
      url: url,
      params: params,
      success: function(res) {
        util.hiddenLoading();
        wx.stopPullDownRefresh();
        if (res.data.code) {
          util.showError(res.data.message);
          return false;
        }
        var paging = res.data.data.paging;
        that.setData({
          page: paging.page,
          total_page: paging.total_page
        })
        var currentSaleDetail = res.data.data.list;
        if (that.data.page == 1) {
          var saleDetailLists = currentSaleDetail;
        } else {
          var saleDetailLists = that.data.sales.concat(currentSaleDetail);
        }
        if (saleDetailLists.length > 0) {
          that.setData({
            sales: saleDetailLists,
            showSaleDetail: true,
          })
        } else {
          that.setData({
            sales: [],
            showSaleDetail: false
          })
        }
      },
      fail: function(res) {
        util.hiddenLoading();
        wx.stopPullDownRefresh();
      }
    })
  },

  // 切换日期
  swichNav: function(e) {
    var that = this;
    var currentTime = e.target.dataset.currentTime;
    this.setData({
      currentTime: currentTime,
      showDateChoose: (currentTime == 0) ? true : false
    });
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function() {
      that.refreshData();
    }, 300)

  },

  //自定义日期是否隐藏
  actionDayChoose: function(e) {
    this.setData({
      inputDateHidden: !this.data.inputDateHidden
    })
  },

  //加载更多
  onReachBottom: function() {
    var that = this;
    if (this.data.page < this.data.total_page) {
      this.setData({
        page: ++this.data.page
      })
      setTimeout(function() {
        that.loadShopSaleDetailData();
      }, 300)
    }
  },

  //下拉刷新
  onPullDownRefresh: function() {
		wx.showLoading({
			title: '加载中',
		})
    this.refreshData();
  },

  //输入开始日期
  startDateChange: function(e) {
    this.setData({
      startDate: e.detail.value
    })
  },

  //输入结束日期
  endDateChange: function(e) {
    this.setData({
      endDate: e.detail.value
    })
  },

  //时间筛选
  confirm: function() {
    var that = this;
    var a = /^(\d{4})-(\d{2})-(\d{2})$/
    if (a.test(this.data.startDate)) {
      var start_time = this.data.startDate;
    }

    if (a.test(this.data.endDate)) {
      var end_time = this.data.endDate;
    }
    this.setData({
      page: 1,
      total_page: 1,
      start_time: start_time,
      end_time: end_time,
      inputDateHidden: true
    })
    setTimeout(function() {
      that.refreshData();
    }, 300)
  },

  //自定义日期取消
  cancel: function() {
    this.setData({
      inputDateHidden: true
    })
  },

	// 使用规则弹框
	showRegulations: function () {
		var that = this;
		if (that.data.income_statement == '暂无') {
			api.POST({
				url: app.globalData.base_url + 'system/set',
				params: {},
				success: function (res) {
					if (res.data.code == '') {
						that.setData({
							income_statement: res.data.data.income_statement,
						})
					}
				}
			})
		}
		that.setData({
			showRegulations: true
		})
	},
	// 使用规则弹框隐藏
	hiddenRegulations: function () {
		this.setData({
			showRegulations: false
		})
	},

  //运营商销售排行顺序切换
  rank_type: function(e) {
    var that = this;
    var ids = e.currentTarget.dataset.ids;
    var rank_type = 11;
    if (ids == 1) {
      if (that.data.rank_type == 11) {
        rank_type = 12
      } else {
        rank_type = 11
      }
    } else {
      if (that.data.rank_type == 21) {
        rank_type = 22
      } else {
        rank_type = 21
      }
    }
    that.setData({
      rank_type: rank_type
    })
    setTimeout(function() {
      that.refreshData();
    }, 300)
  },

	//退款明细
	refundDetail:function(){
		var startDate = (this.data.startDate == '请选择开始日期' ? '' : this.data.startDate);
		var params = 'id=' + this.data.shop_id + '&id_type=' + this.data.shop_type + '&time=' + this.data.currentTime + '&start_time=' + startDate + '&end_time=' + this.data.endDate
		wx.navigateTo({
			url: '/pages/refundDetail/refundDetail?' + params,
		})
	}
})